package com.rederxu.tools;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * DisplayUtils
 * <ul>
 * <li>{@link #getDisplay(Context)}获取显示设备</li>
 * <li>{@link #getScreenWidth(Context)}获取屏幕宽度</li>
 * <li>{@link #getScreenHeight(Context)} 获取屏幕高度大小</li>
 * <li>{@link #dipToPx(Context, float)}dip转换为px</li>
 * <li>{@link #pxToDip(Context, int)}px转换为dip</li>
 * <li>{@link #getDensityDpi(Context)}获取屏幕密度</li>
 * <li>{@link #getStatusHeight(Activity)}获得状态栏高度</li>
 * </ul>
 */
public class DisplayUtils {
	private static final String TAG = DisplayUtils.class.getSimpleName();

	/**
	 * 获取显示设备参数
	 * 
	 * @param context
	 * @return
	 */
	public static Display getDisplay(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay();
	}

	/**
	 * 获取屏幕宽度大小，单位px
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		Display display = getDisplay(context);
		int screenWidth = display.getWidth();
		return screenWidth;
	}

	/**
	 * 获取屏幕高度大小，单位px
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		Display display = getDisplay(context);
		int screenHeight = display.getHeight();
		return screenHeight;
	}

	/**
	 * dip转换为px
	 * 
	 * @param context
	 * @param dip
	 * @return
	 */
	public static int dipToPx(Context context, float dip) {
		return (int) (dip * getDisplayMetrics(context).density + 0.5f);
	}

	/**
	 * px转换为dip
	 * 
	 * @param context
	 * @param px
	 * @return
	 */
	public static int pxToDip(Context context, int px) {
		return (int) (px / getDisplayMetrics(context).density + 0.5f);
	}

	private static DisplayMetrics getDisplayMetrics(Context context) {
		Display display = getDisplay(context);
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		return dm;
	}

	/**
	 * 获取屏幕密度
	 * 
	 * @param context
	 * @return
	 */
	public static int getDensityDpi(Context context) {
		DisplayMetrics dm = getDisplayMetrics(context);
		return dm.densityDpi;
	}

	/**
	 * 获得状态栏高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getStatusHeight(Activity activity) {
		int statusHeight = 0;
		Rect localRect = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
		statusHeight = localRect.top;
		if (0 == statusHeight) {
			Class<?> localClass;
			try {
				localClass = Class.forName("com.android.internal.R$dimen");
				Object localObject = localClass.newInstance();
				int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
				statusHeight = activity.getResources().getDimensionPixelSize(i5);
			} catch (Exception e) {
				Log.w(TAG, e.toString());
			}
		}
		return statusHeight;
	}

	/**
	 * 获取textview文案的宽度
	 * 
	 * @param tv
	 * @return
	 * 		宽度 - px单位
	 */
	public static int getTextStringWidth(TextView tv) {
		Rect bounds = new Rect();
		String text = tv.getText().toString();
		TextPaint paint;

		paint = tv.getPaint();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return bounds.width();
	}
	
	/**
	 * 获取textview文案的宽度
	 * 
	 * @param tv
	 * @return
	 * 		高度 - px单位
	 */
	public static int getTextStringHeight(TextView tv) {
		Rect bounds = new Rect();
		String text = tv.getText().toString();
		TextPaint paint;

		paint = tv.getPaint();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return bounds.height();
	}
	
	/**
	 * 改变TextView部分字体颜色和大小
	 * */
	public static void setTextViewContentDisplay(TextView tv,List<TextViewDisplay> listTextViewDisplay){
		SpannableStringBuilder sb = new SpannableStringBuilder(tv.getText().toString());
		for(TextViewDisplay mTextViewDisplay : listTextViewDisplay){
			if(mTextViewDisplay.type == 0){
				ForegroundColorSpan fcs = new ForegroundColorSpan(mTextViewDisplay.value);
				sb.setSpan(fcs, mTextViewDisplay.start, mTextViewDisplay.over, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
			}else{
				AbsoluteSizeSpan ass = new AbsoluteSizeSpan(mTextViewDisplay.value); // Span to make text size
				sb.setSpan(ass,  mTextViewDisplay.start, mTextViewDisplay.over, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make them also bold
			}
		}
		tv.setText(sb);
	}
	
	public static class TextViewDisplay{
		public int start = 0;
		public int over = 0;
		/***
		 * 0表示改变颜色，1表示大小
		 * */
		public int type = 0;
		public int value = 0x000000;
		
		public static TextViewDisplay getNewInstance(int start,int over,int type,int value){
			return new TextViewDisplay(start, over, type, value);
		}
		
		private TextViewDisplay(int start,int over,int type,int value){
			this.start = start;
			this.over = over;
			this.type = type;
			this.value = value;
		}
	}
}